﻿using System.Collections.Generic;
using System.Data;
using MyFloridaHouse.DataAccess.Extensions;
using MyFloridaHouse.libSvDTO;
using System.Data.SqlClient;
using Microsoft.SqlServer.Types;

namespace MyFloridaHouse.DataAccess
{
    public partial class DAL_CensusData
	{
		#region SQL Data Methods

		/// <summary>
		/// Gets the district geography shape.
		/// </summary>
		/// <param name="iUogIDList">The i uog ID list.</param>
		/// <returns>List of <see cref="SqlGeography"/></returns>
		/// <remarks>SPROC: usp_GetGeographiesFromUogIDList</remarks>
        public List<SqlGeography> GetDistrictGeographyShape(string iUogIDList)
        {
            List<SqlGeography> itemList = new List<SqlGeography>();

            //string storedProcedureName = "usp_GetGeographiesFromUogIDList";
            string storedProcedureName = "usp_tl10GetGeographiesFromUogIDList10";

            using (SqlConnection db = Connect())
            {
                SqlCommand StoredProcedureCommand = new SqlCommand(storedProcedureName, db);
                StoredProcedureCommand.CommandType = CommandType.StoredProcedure;
                SqlParameter dataPartIDParm = StoredProcedureCommand.Parameters.AddWithValue("uogIDList", iUogIDList);
                dataPartIDParm.Direction = ParameterDirection.Input;

                db.Open();
                using (SqlDataReader rdr = StoredProcedureCommand.ExecuteReader())
                {
                    while (rdr.Read())
                    {
                        itemList.Add(DistrictShapeGeography_DataContract.DataContract(rdr));
                    }
                }
            }

            return itemList;
        }

		/// <summary>
		/// Gets the district data summary.
		/// </summary>
		/// <param name="iDistrictID">The idistrictID.</param>
		/// <param name="iRegionIDList">The iregionID list.</param>
		/// <param name="iSourceLayerIDList">The isourcelayerID list.</param>
		/// <param name="iDictionaryIDList">The idictionaryID list.</param>
		/// <returns>List of <see cref="RegionDataDTO"/></returns>
		/// <remarks>SPROC: usp_GetDistrictDataSummary</remarks>
		public List<RegionDataDTO> GetDistrictDataSummary(int iDistrictID, string iRegionIDList, string iSourceLayerIDList, string iDictionaryIDList)
        {
            List<RegionDataDTO> itemList = new List<RegionDataDTO>();

            //string storedProcedureName = "usp_GetDistrictDataSummary";
            string storedProcedureName = "usp_tl10GetDistrictDataSummary10";

            using (SqlConnection db = Connect())
            {
                SqlCommand StoredProcedureCommand = new SqlCommand(storedProcedureName, db);
                StoredProcedureCommand.CommandType = CommandType.StoredProcedure;
                SqlParameter dataPartIDParm = StoredProcedureCommand.Parameters.AddWithValue("iDistrictID", iDistrictID);
                dataPartIDParm.Direction = ParameterDirection.Input;
                SqlParameter dataPartIDParm2 = StoredProcedureCommand.Parameters.AddWithValue("iRegionIDList", iRegionIDList);
                dataPartIDParm2.Direction = ParameterDirection.Input;
                SqlParameter dataPartIDParm3 = StoredProcedureCommand.Parameters.AddWithValue("iSourceLayerIDList", iSourceLayerIDList);
                dataPartIDParm3.Direction = ParameterDirection.Input;
                SqlParameter dataPartIDParm4 = StoredProcedureCommand.Parameters.AddWithValue("iDictionaryIDList", iDictionaryIDList);
                dataPartIDParm4.Direction = ParameterDirection.Input;

                db.Open();
                using (SqlDataReader rdr = StoredProcedureCommand.ExecuteReader())
                {
                    while (rdr.Read())
                    {
                        itemList.Add(RegionDataDTODataContract.DataContract(rdr));
                    }
                }
            }

            return itemList;
        }

		/// <summary>
		/// Gets the file info. District plans are stored as blobs as named objects. This method returns
		/// a list of info object that are used to populate a user screen control. The user select a plan
		/// which is in turn passed to the FileSTO GetFile method as a parameter.
		/// </summary>
		/// <returns>List of <see cref="FileInfoDTO"/></returns>
		/// <remarks>SPROC: usp_GetFileInfo</remarks>
        public List<FileInfoDTO> GetFileInfo()
        {
            List<FileInfoDTO> itemList = new List<FileInfoDTO>();

            //string storedProcedureName = "usp_GetFileInfo";
            string storedProcedureName = "usp_tl10GetFileInfo10";

            SqlConnection db = Connect();
            SqlCommand StoredProcedureCommand = new SqlCommand(storedProcedureName, db);
            StoredProcedureCommand.CommandType = CommandType.StoredProcedure;
            db.Open();
            using (SqlDataReader rdr = StoredProcedureCommand.ExecuteReader())
            {
                while (rdr.Read())
                {
                    itemList.Add(FileInfoDTODataContract.DataContract(rdr));
                }
            }
            return itemList;
        }

		/// <summary>
		/// Gets the district plan types.
		/// </summary>
		/// <returns>List of <see cref="DistrictPlanTypeDTO"/></returns>
		/// <remarks>SPROC: usp_GetDistrictPlanTypes</remarks>
		public List<DistrictPlanTypeDTO> GetDistrictPlanTypes()
		{
			List<DistrictPlanTypeDTO> itemList = new List<DistrictPlanTypeDTO>();

            //string storedProcedureName = "usp_GetDistrictPlanTypes";
            string storedProcedureName = "usp_tl10GetDistrictPlanTypes10";

            using (SqlConnection db = Connect())
            {
                SqlCommand StoredProcedureCommand = new SqlCommand(storedProcedureName, db);
                StoredProcedureCommand.CommandType = CommandType.StoredProcedure;

                db.Open();
                using (SqlDataReader rdr = StoredProcedureCommand.ExecuteReader())
                {
                    while (rdr.Read())
                    {
                        itemList.Add(DistrictPlanTypeDTODataContract.DataContract(rdr));
                    }
                }
            }

			return itemList;
		}
		#endregion

		//Todo: Remove obsolete code.
		#region Obsolete and Unused methods

		//Todo: Is commented out in: DistrictShapeSTO GetDistrictShape(byte[] compressedShapeList, int districtID)
		/// <summary>
		/// [Obsolete] Gets the district geometry shape.
		/// </summary>
		/// <param name="iUogIDList">The iuogID list.</param>
		/// <returns></returns>
		/// <remarks>[NOT Obsolete] SPROC: usp_GetGeometriesFromUogIDList</remarks>
		public List<SqlGeometry> GetDistrictGeometryShape(string iUogIDList)
		{
            List<SqlGeometry> itemList = new List<SqlGeometry>();
            string storedProcedureName = "usp_tl10GetGeometriesFromUogIDList10";
            using (SqlConnection db = Connect())
            {
                SqlCommand StoredProcedureCommand = new SqlCommand(storedProcedureName, db);
                StoredProcedureCommand.CommandType = CommandType.StoredProcedure;
                SqlParameter dataPartIDParm = StoredProcedureCommand.Parameters.AddWithValue("uogIDList", iUogIDList);
                dataPartIDParm.Direction = ParameterDirection.Input;
                db.Open();
                using (SqlDataReader rdr = StoredProcedureCommand.ExecuteReader())
                {
                    while (rdr.Read())
                    {
                        itemList.Add(DistrictShapeGeometry_DataContract.DataContract(rdr));
                    }
                }
            }

            return itemList;
		}

		/// <summary>
		/// [Obsolete] Gets the district shape add.
		/// </summary>
		/// <param name="iPrevWkt">The i prev WKT.</param>
		/// <param name="iUogIDList">The i uog ID list.</param>
		/// <returns></returns>
		/// [Obsolete] <remarks>SPROC: usp_GetDistrictShapeAdd</remarks>
		public DistrictShapeDTO GetDistrictShapeAdd(string iPrevWkt, string iUogIDList)
        {
            DistrictShapeDTO itemList = new DistrictShapeDTO();
            //Database db = DatabaseFactory.CreateDatabase("Reap");
            //DbCommand dbCommand = db.GetStoredProcCommand("usp_GetDistrictShapeAdd");
            //db.AddInParameter(dbCommand, "prevWkt", DbType.String, iPrevWkt);
            //db.AddInParameter(dbCommand, "uogIDList", DbType.String, iUogIDList);
            //using (IDataReader rdr = db.ExecuteReader(dbCommand))
            //{
            //    while (rdr.Read())
            //    {
            //        itemList = DistrictShapeDTODataContract.DataContract(rdr);
            //    }
            //}
            return itemList;
        }

		/// <summary>
		/// [Obsolete] Gets the district shape subtract.
		/// </summary>
		/// <param name="iPrevWkt">The i prev WKT.</param>
		/// <param name="iUogIDList">The i uog ID list.</param>
		/// <returns></returns>
		/// [Obsolete] <remarks>SPROC: usp_GetDistrictShapeSubtract</remarks>
		public DistrictShapeDTO GetDistrictShapeSubtract(string iPrevWkt, string iUogIDList)
        {
            DistrictShapeDTO itemList = new DistrictShapeDTO();
            //Database db = DatabaseFactory.CreateDatabase("Reap");
            //DbCommand dbCommand = db.GetStoredProcCommand("usp_GetDistrictShapeSubtract");
            //db.AddInParameter(dbCommand, "prevWkt", DbType.String, iPrevWkt);
            //db.AddInParameter(dbCommand, "uogIDList", DbType.String, iUogIDList);
            //using (IDataReader rdr = db.ExecuteReader(dbCommand))
            //{
            //    while (rdr.Read())
            //    {
            //        itemList = DistrictShapeDTODataContract.DataContract(rdr);
            //    }
            //}
            return itemList;
		}

		#endregion

	}

}
